<?php


namespace modules\english;


use model\english\EnglishDb;
use modules\es\EnglishEs;

class Review
{

    public static function delete($id)
    {
        return EnglishEs::delete($id);
    }

    /**
     * 主页搜索
     * @param $type
     * @param $keyword
     * @param $p
     * @param $size
     * @return array
     */
    public static function search($type, $keyword, $p, $size)
    {


        $list = [
            [
                'id' => 50,
                'word' => 50,
            ]
        ];

        return compact('list', 'total', 'size');
    }

    public static function getInfo2Db($id)
    {
        $rs = EnglishDb::get('*', ['id' => $id]);

        return new \entity\db\EntityEnglish($rs);
    }

    public static function add2Db(\lib\entity\es\EntityEnglish $entity)
    {
        $dbEntity = new \lib\entity\db\EntityEnglish();
        $dbEntity->id = $entity->id;
        $dbEntity->title = $entity->title;
        $dbEntity->type = $entity->type;
        $dbEntity->content = $entity;

        return EnglishDb::save($dbEntity);
    }

    public static function getEnglishTypeMap($toString = false, $withAll = false)
    {
        $map = EnglishDb::TYPE_MAP;
        if ($withAll) array_unshift($map, EnglishDb::ALL_TYPE);
        return $toString ? json_encode($map) : $map;
    }

    public static function getItem($id)
    {
        if (!strlen($id)) return null;

        $info = EnglishEs::getInfo($id);

        $info['content'] = self::formatContent($info['content'], EnglishEs::FORMAT_TYPE_ES_TO_EDIT);

        return $info;
    }

    public static function formatContent($content, $formatType = EnglishEs::FORMAT_TYPE_HTML_TO_ES)
    {
        switch ($formatType) {
            case EnglishEs::FORMAT_TYPE_HTML_TO_ES:
                $content = EnglishEs::html2es($content);
                break;
            case EnglishEs::FORMAT_TYPE_ES_TO_EDIT:
                $content = EnglishEs::es2html($content);
                break;
            case EnglishEs::FORMAT_TYPE_ES_TO_SEARCH:
                $content = EnglishEs::es2search($content);
                break;
        }
        return $content;
    }

    /**
     * @param \lib\entity\db\EntityEnglish $info
     * @return array
     * @throws \ReflectionException
     */
    public static function add2Es(\lib\entity\db\EntityEnglish $info)
    {
        /**
         * @var \lib\entity\es\EntityEnglish $content
         */
        $content = json_decode($info->content);

        $entity = new \lib\entity\es\EntityEnglish;
        $entity->id = $info->id;
        $entity->title = $info->title;
        $entity->type = $info->type;
        $entity->tags = $content->tags;
        $entity->content = $content->content;
        $entity->addTime = date("Y-m-d H:i:s", strtotime($info->addTime));

        return EnglishEs::index($entity);
    }

    public static function format($value, $style = 'info')
    {
        $valueArr = is_array($value) ? $value : [$value];
        $valueArr = array_map(function($item) use ($style) {
            return strlen($item) ? "<span class='badge badge-{$style} '>$item</span>" : '';
        }, $valueArr);
        return implode(" ", $valueArr);
    }
}